package com.cqs.learning.spring.cloud.producer.web.controller;

import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by li on 20-2-29.
 */
@RequestMapping
@RestController
@Log4j2
public class Product2Controller implements BeanPostProcessor {

    private AtomicInteger cnt = new AtomicInteger();


    @Value("${config.test}")
    private String configTest;

    @Value("${eureka.instance.appname}")
    private String instanceName;


    @PostConstruct
    public void test55() {
        log.error("=== configTes{},instanceName:{}", configTest, instanceName);
    }


    @RequestMapping("/cnt")
    public String cnt() {
        int count = test();
        try {
            TimeUnit.MILLISECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "product2 cnt:" + count;
    }

    //异步
    @RequestMapping("/cnt2")
    public DeferredResult<String> executeSlowTask() {
        DeferredResult<String> deferredResult = new DeferredResult<>();
        CompletableFuture.runAsync(this::test).whenCompleteAsync((result, throwable) -> {
            throwable.printStackTrace();
            log.error("放生异常了");
            deferredResult.setResult(String.valueOf(result));
        });
        log.info(Thread.currentThread().getName() + "异步处理结束");
        return deferredResult;
    }

    private int test() {
        for (int i = 0; i < 100; i++) {
            try {
                FileUtils.readFileToByteArray(new File("E:\\idea\\spring-cloud\\producer2\\src\\main\\resources\\application.yml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        log.info("当前线程：" + Thread.currentThread() + "处理结束");
        return cnt.incrementAndGet();
    }


    private void test2() {
        DeferredResult<String> deferredResult = new DeferredResult<>();
        CompletableFuture.supplyAsync(this::test).whenCompleteAsync((result, throwable) -> {
            throwable.printStackTrace();
            log.error("放生异常了");
            deferredResult.setResult(String.valueOf(result));
        });
    }


    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
